package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.TranslateAnimation;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.xgkj.diyiketang.EatShowApp;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.AnchorActivity;
import com.xgkj.diyiketang.activity.BangDanActivity;
import com.xgkj.diyiketang.data.AnchorInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.TouPiaoTask;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.PalLog;
import com.xgkj.diyiketang.util.ToastUtil;
import com.xgkj.diyiketang.util.ViewUtil;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by XCKJ_Paladowow on 2016/3/15.
 */
public class BangDanAdatper extends BaseAdapter {
    private Context mContext;
    private List<AnchorInfo> mList = new ArrayList<AnchorInfo>();
    private int toupiaoTaskId = -1;
    private String anchorid;
    private String gameid;
    private int golbalPosition = -1;

    public String getGameid() {
        return gameid;
    }

    public void setGameid(String gameid) {
        this.gameid = gameid;
    }

    public BangDanAdatper(Context context) {
        mContext = context;
    }
    public void updateData(List<AnchorInfo> list){
        if(mList != null){
            mList.addAll(list);
        }
        notifyDataSetChanged();
    }

    public void clearData(){
        if(mList != null){
            mList.clear();
        }
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        if(mList != null){
            return mList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if(mList != null){
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ChildHolder childHolder = null;
        if(convertView == null || convertView.getTag()== null){
            childHolder = new ChildHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.bangdan_item,null);
            childHolder.zhuboImg = (ImageView) convertView.findViewById(R.id.bangdan_icon);
            childHolder.voteImg = (ImageView) convertView.findViewById(R.id.bangdan_voteimg);
            childHolder.zhuboNick = (TextView) convertView.findViewById(R.id.bangdan_nick);
            childHolder.zhuboSign = (TextView) convertView.findViewById(R.id.bangdan_sign);
            childHolder.voteNum = (TextView) convertView.findViewById(R.id.bangdan_votenum);
            childHolder.paiming = (TextView) convertView.findViewById(R.id.bangdan_num);
            childHolder.zhuboCode = (TextView) convertView.findViewById(R.id.bangdan_code);

            convertView.setTag(childHolder);
        }else{
            childHolder = (ChildHolder) convertView.getTag();
        }

        final AnchorInfo info = mList.get(position);
        if(info != null){
//            anchorid = info.getAnchorId();
//            gameid = getGameid();
            String anchorImgUrl = info.getAnchorLogo();
            if(anchorImgUrl != null && anchorImgUrl.length()>0){
                ImageCache.getInstance(mContext).loadBitmapToView(childHolder.zhuboImg,anchorImgUrl,new ViewUtil.Size(ViewUtil.dip2px(mContext,40),ViewUtil.dip2px(mContext,40)));
            }
            childHolder.zhuboImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mContext.startActivity(AnchorActivity.getIntent(mContext, info.getAnchorId()));
                }
            });

            if(info.getNickName()!=null){
                try {
                    childHolder.zhuboNick.setText(URLDecoder.decode(info.getNickName(), "utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            if(info.getAnchorSign()!=null){
                try {
                    childHolder.zhuboSign.setText(URLDecoder.decode(info.getAnchorSign(), "utf-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
//           childHolder.zhuboNick.setText(info.getNickName());
//            childHolder.zhuboSign.setText(info.getAnchorSign());
            childHolder.voteNum.setText("已有"+info.getAnchorVote()+"人投票");
            childHolder.zhuboCode.setText("NO."+info.getAnchorCode());
            int ph =position+1;
//            if(ph==1){
//                childHolder.paiming.setBackgroundResource(R.drawable.top1);
//                childHolder.paiming.setText("");
//            }else if(ph==2){
//                childHolder.paiming.setBackgroundResource(R.drawable.top2);
//                childHolder.paiming.setText("");
//            }else if(ph==3){
//                childHolder.paiming.setBackgroundResource(R.drawable.top3);
//                childHolder.paiming.setText("");
//            }else{
//                childHolder.paiming.setBackgroundResource(0);
                childHolder.paiming.setText(ph+"");
//            }

            if(info.getIsVote().equals("0")){
                childHolder.voteImg.setBackgroundResource(R.drawable.bd_toupiao_checked);
                final ChildHolder finalChildHolder = childHolder;
                childHolder.voteImg.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        finalChildHolder.voteImg.setBackgroundResource(R.drawable.bd_toupiao_unchecked);
                        finalChildHolder.voteImg.setClickable(false);
                        golbalPosition = position;
                        doTouPiao(position);

                    }
                });

            }else {
                childHolder.voteImg.setBackgroundResource(R.drawable.bd_toupiao_unchecked);
                childHolder.voteImg.setClickable(false);
            }



        }



        return convertView;
    }

    private void doTouPiao(int position){
        AnchorInfo aiinfo = mList.get(position);
        anchorid = aiinfo.getAnchorId();
        gameid = getGameid();
        //PalLog.e("主播的ID###########",anchorid);
        TouPiaoTask tpTask = new TouPiaoTask(anchorid,gameid);
        tpTask.addListener(toupiaoListener);
        toupiaoTaskId = EatShowApp.getInstance().getTaskManager().addTask(tpTask);
    }

    private TaskEventListener toupiaoListener = new ShowToastTaskEventListener(mContext){
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            toupiaoTaskId = -1;
            if(golbalPosition!= -1){
                AnchorInfo ainfo = mList.get(golbalPosition);
                int add = Integer.valueOf(ainfo.getAnchorVote())+1;

                ainfo.setAnchorVote(String.valueOf(add));
                   notifyDataSetChanged();
            }



        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            toupiaoTaskId = -1;

        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }
    };

    class ChildHolder{
        ImageView voteImg;
        ImageView zhuboImg;
        TextView zhuboNick;
        TextView zhuboSign;
        TextView voteNum;
        TextView paiming;
        TextView zhuboCode;

    }
}
